package com.springboot.service;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.math.BigDecimal;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Map;

import java.util.Calendar;

import java.util.Collections;

import java.util.Date;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import com.springboot.dao.FurniturepinglunMapper;

import com.springboot.entity.Furniturepinglun;

import com.springboot.entity.FurniturepinglunExample;

import com.springboot.entity.*;
import com.springboot.dao.*;
import com.springboot.entity.Furniture;

import com.springboot.entity.FurnitureExample;

import com.springboot.dao.FurnitureMapper;

import com.springboot.entity.User;

import com.springboot.entity.UserExample;

import com.springboot.dao.UserMapper;

import com.springboot.entity.User;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.multipart.MultipartFile;

@Service

public class FurniturepinglunService implements FurniturepinglunServiceImp {


    private static final Log logger = LogFactory.getLog(FurniturepinglunService.class);

    @Autowired
    private FurniturepinglunMapper furniturepinglundao;
// 定义furnituredao参数  


    @Autowired
    private FurnitureMapper furnituredao;
// 定义userdao参数  


    @Autowired
    private UserMapper userdao;


    //定义方法addfurniturepinglun,响应控制层addfurniturepinglun请求

    public void addfurniturepinglun(HttpServletRequest request, HttpSession session) {

        //输出日志，当前执行方法为addfurniturepinglun

        logger.debug("FurniturepinglunService.addfurniturepinglun ......");

// 定义 example1为 FurnitureExample的实例

        FurnitureExample example1 = new FurnitureExample();

// 定义furnitureall的List使用 furnituredao的selectByExample方法查询所有家具评论数据

        List furnitureall = furnituredao.selectByExample(example1);

        // 将上面查询到的家具评论数据保存到request中，返回页面

        request.setAttribute("furnitureall", furnitureall);

// 定义 example2为 UserExample的实例

        UserExample example2 = new UserExample();

// 定义userall的List使用 userdao的selectByExample方法查询所有家具评论数据

        List userall = userdao.selectByExample(example2);

        // 将上面查询到的家具评论数据保存到request中，返回页面

        request.setAttribute("userall", userall);


    }


    //定义useraddfurniturepinglun方法，在家具评论控制层中调用

    public void useraddfurniturepinglun(HttpServletRequest request, HttpSession session) {

        //输出日志，表示当前正在执行FurniturepinglunService.useraddfurniturepinglun方法

        logger.debug("FurniturepinglunService.useraddfurniturepinglun ......");

// 定义 example1为 FurnitureExample的实例

        FurnitureExample example1 = new FurnitureExample();

// 定义furnitureall的List使用 furnituredao的selectByExample方法查询所有家具评论数据

        List furnitureall = furnituredao.selectByExample(example1);

        // 将上面查询到的家具评论数据保存到request中，返回页面

        request.setAttribute("furnitureall", furnitureall);

// 定义 example2为 UserExample的实例

        UserExample example2 = new UserExample();

// 定义userall的List使用 userdao的selectByExample方法查询所有家具评论数据

        List userall = userdao.selectByExample(example2);

        // 将上面查询到的家具评论数据保存到request中，返回页面

        request.setAttribute("userall", userall);


    }


    // 定义userupdatefurniturepinglun方法

    public void userupdatefurniturepinglun(HttpServletRequest request, HttpSession session, int id) {

        // 输出日志FurniturepinglunService.userupdatefurniturepinglun

        logger.debug("FurniturepinglunService.userupdatefurniturepinglun ......");


        // 实例化家具评论,使用furniturepinglundao的selectByPrimaryKey方法通过id查询家具评论

        Furniturepinglun furniturepinglun = furniturepinglundao.selectByPrimaryKey(id);


// 定义 example1为 FurnitureExample的实例

        FurnitureExample example1 = new FurnitureExample();

// 定义furnitureall的List使用 furnituredao的selectByExample方法查询所有家具评论数据

        List furnitureall = furnituredao.selectByExample(example1);

        // 将上面查询到的家具评论数据保存到request中，返回页面

        request.setAttribute("furnitureall", furnitureall);

// 定义 example2为 UserExample的实例

        UserExample example2 = new UserExample();

// 定义userall的List使用 userdao的selectByExample方法查询所有家具评论数据

        List userall = userdao.selectByExample(example2);

        // 将上面查询到的家具评论数据保存到request中，返回页面

        request.setAttribute("userall", userall);


        // 将查询出的家具评论信息保存到request中

        request.setAttribute("furniturepinglun", furniturepinglun);


    }


    // 定义userfurniturepinglunmanage方法

    public void userfurniturepinglunmanage(HttpServletRequest request, HttpSession session) {

        // 输出日志，表示当前正在执行方法FurniturepinglunService.userfurniturepinglunmanage

        logger.debug("FurniturepinglunService.userfurniturepinglunmanage ......");


        // 实例化为user,并从session中取出当前登录用户赋值，

        User user = (User) session.getAttribute("userinfo");


        // 实例化FurniturepinglunExample为example

        FurniturepinglunExample example = new FurniturepinglunExample();


        // 实例化FurniturepinglunExample的Criteria内部类为criteria

        FurniturepinglunExample.Criteria criteria = example.createCriteria();

        // 定义查询条件的pinglunrenid为当前登录用户id

        criteria.andPinglunrenidEqualTo(user.getId());


        // 查询当前用户所有的 家具评论，保存到furniturepinglunall中

        List furniturepinglunall = furniturepinglundao.selectByExample(example);


        // 将查询出的 家具评论保存到request中的furniturepinglunall中

        request.setAttribute("furniturepinglunall", furniturepinglunall);

    }


    //定义useraddfurniturepinglunact，响应页面添加请求

    public void useraddfurniturepinglunact(HttpServletRequest request, HttpSession session, Furniturepinglun furniturepinglun) throws IOException {

        //输出日志，表示当前正在执行方法为FurniturepinglunService.useraddfurniturepinglunact

        logger.debug("FurniturepinglunService.useraddfurniturepinglunact ......");

        //使用mybatis逆向工程所提供的insert方法添加家具评论数据到数据库中

        furniturepinglundao.insert(furniturepinglun);


        //将添加家具评论成功的信息保存到request中，在页面中进行初始化

        request.setAttribute("message", "添加家具评论成功");

    }


    //定义addfurniturepinglunact方法

    public void addfurniturepinglunact(HttpServletRequest request, HttpSession session, Furniturepinglun furniturepinglun) throws IOException {

        //输出当前方法日志，表示正在执行FurniturepinglunService.addfurniturepinglunact方法

        logger.debug("FurniturepinglunService.addfurniturepinglunact ......");

        //使用furniturepinglundao的insert方法将家具评论添加到数据库中

        furniturepinglundao.insert(furniturepinglun);


        //将添加家具评论信息添加到request中用message参数进行保存

        request.setAttribute("message", "添加家具评论成功");

    } //定义addfurniturepinglunactjson方法

    public void addfurniturepinglunactjson(Furniturepinglun furniturepinglun) throws IOException {

        //输出当前方法日志，表示正在执行FurniturepinglunService.addfurniturepinglunactjson方法

        logger.debug("FurniturepinglunService.addfurniturepinglunactjson ......");

        //使用furniturepinglundao的insert方法将家具评论添加到数据库中

        furniturepinglundao.insert(furniturepinglun);

    }


    //定义furniturepinglunmanage方法响应页面请求

    public void furniturepinglunmanage(HttpServletRequest request, HttpSession session) {

        //输出日志，表示当前正在执行FurniturepinglunService.furniturepinglunmanage

        logger.debug("FurniturepinglunService.furniturepinglunmanage ......");


        //实例化FurniturepinglunExample类为example

        FurniturepinglunExample example = new FurniturepinglunExample();


        //使用  furniturepinglundao.selectByExample(example)方法获取所有的家具评论数据

        List furniturepinglunall = furniturepinglundao.selectByExample(example);


        //将所有的家具评论数据保存到request中的furniturepinglunall参数里

        request.setAttribute("furniturepinglunall", furniturepinglunall);


    }


    // 定义 furniturepinglunview方法

    public void furniturepinglunview(HttpServletRequest request, HttpSession session) {

        // 输出日志，表示当前正在执行FurniturepinglunService.furniturepinglunview方法

        logger.debug("FurniturepinglunService.furniturepinglunview ......");


        // 实例化FurniturepinglunExample类

        FurniturepinglunExample example = new FurniturepinglunExample();


        // 使用furniturepinglundao的selectByExample方法查询家具评论信息

        List furniturepinglunall = furniturepinglundao.selectByExample(example);


        // 将查询的furniturepinglunall保存到request中，记录为参数furniturepinglunall

        request.setAttribute("furniturepinglunall", furniturepinglunall);


    }


    // 定义 updatefurniturepinglun方法

    public void updatefurniturepinglun(HttpServletRequest request, HttpSession session, int id) {

        // 输出日志FurniturepinglunController.updatefurniturepinglun，表示正在执行该方法

        logger.debug("FurniturepinglunController.updatefurniturepinglun ......");


        // 定义家具评论为biaoming，使用 furniturepinglundao的selectByPrimaryKey方法通过id查询数据

        Furniturepinglun furniturepinglun = furniturepinglundao.selectByPrimaryKey(id);


// 定义 example1为 FurnitureExample的实例

        FurnitureExample example1 = new FurnitureExample();

// 定义furnitureall的List使用 furnituredao的selectByExample方法查询所有家具评论数据

        List furnitureall = furnituredao.selectByExample(example1);

        // 将上面查询到的家具评论数据保存到request中，返回页面

        request.setAttribute("furnitureall", furnitureall);

// 定义 example2为 UserExample的实例

        UserExample example2 = new UserExample();

// 定义userall的List使用 userdao的selectByExample方法查询所有家具评论数据

        List userall = userdao.selectByExample(example2);

        // 将上面查询到的家具评论数据保存到request中，返回页面

        request.setAttribute("userall", userall);


        // 将查询的家具评论保存到request中

        request.setAttribute("furniturepinglun", furniturepinglun);


    }


    // 定义userupdatefurniturepinglunact方法

    public void userupdatefurniturepinglunact(HttpServletRequest request, Furniturepinglun furniturepinglun, HttpSession session) throws IOException {

        // 当执行此方法时，输出此日志

        logger.debug("FurniturepinglunService.userupdatefurniturepinglunact ......");

        // 调用furniturepinglundao的updateByPrimaryKeySelective方法修改传入的furniturepinglun

        furniturepinglundao.updateByPrimaryKeySelective(furniturepinglun);


        request.setAttribute("message", "修改家具评论信息成功");


    }
    // 定义updatefurniturepinglunact处理家具评论修改

    public void updatefurniturepinglunact(HttpServletRequest request, Furniturepinglun furniturepinglun, HttpSession session) throws IOException {

        // 输出日志，表示正在执行当前方法FurniturepinglunController.updatefurniturepinglunact

        logger.debug("FurniturepinglunController.updatefurniturepinglunact ......");

        // 执行furniturepinglundao的updateByPrimaryKeySelective方法，将传入的家具评论数据同步到数据库中

        furniturepinglundao.updateByPrimaryKeySelective(furniturepinglun);


        // 将修改成功信息保存到request中

        request.setAttribute("message", "修改家具评论信息成功");


    } // 定义updatefurniturepinglunactjson处理家具评论修改

    public void updatefurniturepinglunactjson(Furniturepinglun furniturepinglun) throws IOException {

        // 输出日志，表示正在执行当前方法FurniturepinglunController.updatefurniturepinglunactjson

        logger.debug("FurniturepinglunController.updatefurniturepinglunactjson ......");

        // 执行furniturepinglundao的updateByPrimaryKeySelective方法，将传入的家具评论数据同步到数据库中

        furniturepinglundao.updateByPrimaryKeySelective(furniturepinglun);

    }


    // 定义deletefurniturepinglun,处理删除家具评论

    public void deletefurniturepinglun(HttpServletRequest request, HttpSession session, int id) {

        // 输出日志，表示当前正在执行FurniturepinglunService.deletefurniturepinglun方法

        logger.debug("FurniturepinglunService.deletefurniturepinglun ......");

        //如果传入的id为-1，表示进行批量删除
        if (id == -1) {
            //实例化家具评论example
            FurniturepinglunExample example = new FurniturepinglunExample();
            //获取到传递过来的ids数据信息
            String ids = request.getParameter("ids");
            //为id信息添加-1数据，添加此数据可以防止数据为空时的报错
            ids += "-1,";
            //截取掉多余的逗号
            ids.substring(0, ids.length() - 1);
            //设置where语句进行删除id数据的传入
            //example.setWhere("id in ("+ ids +")");
            //实例化家具评论criteria
            FurniturepinglunExample.Criteria criteria = example.createCriteria();
            //设置integer类型的数组，保存需要删除的家具评论id信息
            List<Integer> idlist = new ArrayList();
            //根据id分离出需要删除的数据id信息
            String[] idarr = ids.split(",");
            //将对应的id数据信息保存到数组中
            for (int i = 0; i < idarr.length; i++) {
                //取出对应的id信息，并转换成int类型
                idlist.add(Integer.parseInt(idarr[i]));
            }
            //传入需要删除的id数组信息
            criteria.andIdIn(idlist);
            //调用家具评论的dao类，执行批量删除操作
            furniturepinglundao.deleteByExample(example);
        } else {
            // 根据id调用furniturepinglundao的deleteByPrimaryKey方法，删除家具评论


            furniturepinglundao.deleteByPrimaryKey(id);

        }


        request.setAttribute("message", "删除家具评论成功");


    }


    // 定义userdeletefurniturepinglun方法

    public void userdeletefurniturepinglun(HttpServletRequest request, HttpSession session, int id) {

        // 输出日志，表示当前正在执行FurniturepinglunController.userdeletefurniturepinglun方法

        logger.debug("FurniturepinglunController.userdeletefurniturepinglun ......");


        // 调用furniturepinglundao的deleteByPrimaryKey方法，传入id。删除id所对应的家具评论

        furniturepinglundao.deleteByPrimaryKey(id);

        // 将删除家具评论成功信息保存到request中


        request.setAttribute("message", "删除家具评论成功");

    }


    // 定义searchfurniturepinglun方法，处理搜索操作

    public void searchfurniturepinglun(HttpServletRequest request, HttpSession session, String search) {

        // 输出日志，表示当前正在执行FurniturepinglunService.searchfurniturepinglun

        logger.debug("FurniturepinglunService.searchfurniturepinglun ......");


        // 实例化 FurniturepinglunExample

        FurniturepinglunExample example = new FurniturepinglunExample();

        // 实例化 FurniturepinglunExample的Criteria内部类

        FurniturepinglunExample.Criteria criteria = example.createCriteria();

        // 如果搜索内容不等于空

        if (search != null) {

            // 将搜索内容通过like关键字传到数据库中进行搜索

            criteria.andFurnitureLike("%" + search + "%");

        }

        // 查询家具评论结果

        List furniturepinglunall = furniturepinglundao.selectByExample(example);

        // 将查询到的家具评论 保存到request中进行保存

        request.setAttribute("furniturepinglunall", furniturepinglunall);

    }


    // 定义searchfurniturepinglunjson方法，处理搜索操作

    public Map searchfurniturepinglunjson(String search) {

        // 输出日志，表示当前正在执行FurniturepinglunService.searchfurniturepinglunjson

        logger.debug("FurniturepinglunService.searchfurniturepinglunjson ......");

        // 定义返回结果

        Map result = new HashMap();

        // 实例化 FurniturepinglunExample

        FurniturepinglunExample example = new FurniturepinglunExample();

        // 实例化 FurniturepinglunExample的Criteria内部类

        FurniturepinglunExample.Criteria criteria = example.createCriteria();

        // 如果搜索内容不等于空

        if (search != null) {

            // 将搜索内容通过like关键字传到数据库中进行搜索

            criteria.andFurnitureLike("%" + search + "%");

        }

        // 查询家具评论结果

        List furniturepinglunall = furniturepinglundao.selectByExample(example);

        // 将查询到的家具评论 保存到request中进行保存

        result.put("furniturepinglunall", furniturepinglunall);

        // 返回查询结果resultmap

        return result;

    }


    // 定义FurniturepinglunpinglunMapper

    public void furniturepinglundetails(HttpServletRequest request, HttpSession session, int id) {

        // 输入日志信息，表名当前执行方法为FurniturepinglunController.furniturepinglundetails

        logger.debug("FurniturepinglunController.furniturepinglundetails ......");


        // 传入页面所传入的家具评论id使用furniturepinglundao的selectByPrimaryKey方法进行查询

        Furniturepinglun furniturepinglun = furniturepinglundao.selectByPrimaryKey(id);

        // 将查询到的家具评论保存到request中

        request.setAttribute("furniturepinglun", furniturepinglun);


    }


    // 定义Furniturepinglun详情方法

    public Map furniturepinglundetailsjson(int id) {

        // 输入日志信息，表名当前执行方法为FurniturepinglunController.furniturepinglundetailsjson

        logger.debug("FurniturepinglunController.furniturepinglundetailsjson ......");


        // 定义返回结果

        Map result = new HashMap();

        // 传入页面所传入的家具评论id使用furniturepinglundao的selectByPrimaryKey方法进行查询

        Furniturepinglun furniturepinglun = furniturepinglundao.selectByPrimaryKey(id);

        // 将查询到的家具评论保存到result中

        result.put("furniturepinglun", furniturepinglun);


        // 返回查询结果resultmap

        return result;

    }


//	上传文件图片等

    public String uploadUtile(MultipartFile file, HttpServletRequest request) throws IOException {

        // 根据当前时间生成时间字符串

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");

        String res = sdf.format(new Date());

        // uploads文件夹位置

        String tomcatroot = request.getSession().getServletContext().getRealPath("resource/uploads/");
        String localroot = request.getServletContext().getRealPath("").split("out")[0] + "WebRoot/resource/uploads";
        System.out.println(tomcatroot);
        System.out.println(localroot);

        // 原始名称

        String originalFileName = file.getOriginalFilename();

        // 新文件名

        String newFileName = "sliver" + res + originalFileName.substring(originalFileName.lastIndexOf("."));

        // 创建年月文件夹

        Calendar date = Calendar.getInstance();

        File dateDirs = new File(date.get(Calendar.YEAR) + File.separator + (date.get(Calendar.MONTH) + 1));

        // 新文件

        File tomcatFile = new File(tomcatroot + File.separator + dateDirs + File.separator + newFileName);
        File localFile = new File(localroot + File.separator + dateDirs + File.separator + newFileName);

        // 判断目标文件所在目录是否存在

        if (!tomcatFile.getParentFile().exists()) {

            // 如果目标文件所在的目录不存在，则创建父目录

            tomcatFile.getParentFile().mkdirs();

        }
        // 判断目标文件所在目录是否存在

        if (!localFile.getParentFile().exists()) {

            // 如果目标文件所在的目录不存在，则创建父目录

            localFile.getParentFile().mkdirs();

        }

        file.transferTo(tomcatFile);

        String fileUrl = date.get(Calendar.YEAR) + "/" + (date.get(Calendar.MONTH) + 1) + "/" + newFileName;

        return fileUrl;

    }
}

